home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games Extra 1996 June / Amiga Games Extra 1996 #6.iso / userbox / publicdomain / cuckoo / source / cuckoo.c next >
C/C++ Source or Header  |  1993-10-06  |  4KB  |  150 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <libraries/dos.h>
  4. #include <devices/audio.h>
  5.  
  6. struct Voice8Header
  7. {
  8.     ULONG   oneShotHiSamples, repeatHiSamples, samplesPerHiCycle;
  9.     UWORD   samplesPerSec;
  10.     UBYTE   ctOctave, sCompression;
  11.     LONG    volume;
  12. };
  13.  
  14. // This stuff is declared somewhere else
  15. extern struct IOAudio *PlaySound();
  16. extern __far BYTE Cuckoo[];
  17. extern void ToggleLED(void);
  18. extern BOOL GetLEDStatus(void);
  19.  
  20. BOOL Find8SVXInformation(BYTE *buffer, BYTE **sample, ULONG *len, UWORD *rate);
  21.  
  22. char txt[] = "$VER: Cuckoo 1.2 (6.10.93) by Anders Melchiorsen\n"
  23.          "Cuckoo will, when started at ??:00 or ??:30, play a cuckoo sample.\n";
  24.  
  25. main()
  26. {
  27.     UWORD cuck_rate;
  28.     ULONG cuck_len,repeat;
  29.     BYTE *cuck_start;
  30.     struct IOAudio *id;
  31.     struct DateStamp now;
  32.     WORD hour, minute;
  33.     BOOL ledon;
  34.  
  35.     // Just to make me famous:
  36.     Write( Output(), &txt[6], strlen(&txt[6]) );
  37.  
  38.     // Get current time
  39.     DateStamp(&now);
  40.     hour   = now.ds_Minute/60;
  41.     minute = now.ds_Minute%60;
  42.  
  43.     // Dicede how many times to call
  44.     repeat = 0;
  45.     if (minute == 0)  repeat = ((hour % 12) ? (hour % 12) : 12);
  46.     if (minute == 30) repeat = 1;
  47.  
  48.     // No call -> exit
  49.     if (repeat == 0)
  50.     return(0);
  51.  
  52.     // Find out where sample starts, how long it is, and how fast it should be
  53.     // played
  54.     if (!Find8SVXInformation(Cuckoo, &cuck_start, &cuck_len, &cuck_rate))
  55.     return(20);
  56.  
  57.     // Turn the low-pass filter off
  58.     ledon = GetLEDStatus();
  59.     if (ledon)
  60.     ToggleLED();
  61.  
  62.     // Play sample the right number of times, delay for half a sec between
  63.     // each cuckoo.
  64.     while (repeat--)
  65.     {
  66.     ULONG signals = NULL;
  67.  
  68.     if (id = PlaySound(cuck_start, cuck_len, 1, 3579545/cuck_rate, 64))
  69.     {
  70.         signals = Wait((1 << id->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit) | SIGBREAKF_CTRL_C);
  71.         StopSound(id);
  72.     }
  73.  
  74.     // Don't repeat anymore if ^C was pressed
  75.     if (signals & SIGBREAKF_CTRL_C)
  76.         repeat = 0;
  77.  
  78.     if (repeat)
  79.         Delay(25);
  80.     }
  81.  
  82.     // Turn filter back on (if it was shut off)
  83.     if (ledon)
  84.     ToggleLED();
  85.     return(0);
  86. }
  87.  
  88. // Finds out of certain 8SVX stuff from a buffer in memory
  89. // Returns TRUE on success
  90. //
  91. // Must be initialized
  92. //  buffer = Pointer to complete 8SVX 'file'
  93. //
  94. // The following are filled in by this routine:
  95. //  sample = Where actual sample starts
  96. //  len    = Length of sample
  97. //  rate   = The samples rate
  98. //
  99. // Don't get confused; it IS a weird pointer example :-)
  100.  
  101. BOOL Find8SVXInformation(BYTE *buffer, BYTE **sample, ULONG *len, UWORD *rate)
  102. {
  103.     LONG jump;
  104.     BOOL foundVHDR = FALSE, foundBODY = FALSE;
  105.     struct Voice8Header *Header;
  106.  
  107.     // Pass 'FORM....'
  108.     buffer += 8;
  109.  
  110.     // Is this a 8SVX file?
  111.     if ( strncmp(buffer, "8SVX", 4) != 0)
  112.     return(FALSE);
  113.  
  114.     // Pass '....' (size of 8SVX; should perhaps use this. Oh well...)
  115.     buffer += 4;
  116.  
  117.     // Find VHDR and BODY chunks and get the info needed from them
  118.     // If one of them is missing I keep on searching forever ... not good :-(
  119.     do
  120.     {
  121.     if (strncmp(buffer, "VHDR", 4) == 0)
  122.     {
  123.         Header = buffer + 8;
  124.         *len = Header->oneShotHiSamples + Header->repeatHiSamples;
  125.         *rate = Header->samplesPerSec;
  126.         foundVHDR = TRUE;
  127.     }
  128.  
  129.     if (strncmp(buffer, "BODY", 4) == 0)
  130.     {
  131.         *sample = buffer + 8;
  132.         foundBODY = TRUE;
  133.     }
  134.  
  135.     buffer += 4;     // Pass chunk ID
  136.  
  137.     // Read chuck size
  138.     jump = (*(buffer)   << 24)
  139.          + (*(buffer+1) << 16)
  140.          + (*(buffer+2) << 8)
  141.          + *(buffer+3);
  142.  
  143.     buffer += 4;     // Pass chunk size information
  144.     buffer += jump;  // Pass rest of chunk (actual data)
  145.     } while ( (foundVHDR==FALSE) || (foundBODY==FALSE) );
  146.  
  147.     return(TRUE);
  148. }
  149.  
  150.